import Vue from 'vue'
import Router from 'vue-router'
import storejs from 'store'
import STORECONST from '../common/storeConst'
import store from '@/store'
import { PAGELOADING } from '@/store/mutation-types'
// import {Loading} from 'vue-ydui/dist/lib.rem/dialog'

const index = r => require.ensure([], () => r(require('@/pages/index/Index')), 'Index')
const model = r => require.ensure([], () => r(require('@/pages/index/model')), 'model')
const Login = r => require.ensure([], () => r(require('@/pages/login/Login')), 'Login')

/**
 * 字典管理
 */
const dic = r => require.ensure([], () => r(require('@/pages/dic/dic_manage')), 'dic_manage')

/**
 * 系统管理
 */
const office = r => require.ensure([], () => r(require('@/pages/system/office')), 'office');//机构列表
const url = r => require.ensure([], () => r(require('@/pages/system/auth/url')), 'url');//接口列表
const mark = r => require.ensure([], () => r(require('@/pages/system/auth/mark')), 'mark');//自定义权限标识列表
const menu = r => require.ensure([], () => r(require('@/pages/system/menu')), 'menu')//菜单管理
const auth = r => require.ensure([], () => r(require('@/pages/system/auth')), 'auth')//权限管理
const role = r => require.ensure([], () => r(require('@/pages/system/role/roleList')), 'role')//角色管理
const user = r => require.ensure([], () => r(require('@/pages/system/user/user')), 'user')//用户管理


let router = new Router({
	routes: [
		{
			path: '/',
			redirect: '/factory',
		},
		{
			path: '/factory',
			component: model,
			children: [
				{
					path: '',
					component: index
				},
				{//菜单管理
					path: 'menu',
					component: menu
				},
				{//字典管理
					path: 'dic',
					component: dic
				},
				{//组织机构
					path: 'office',
					component: office
				},
				{//接口管理
					path: 'url',
					component: url
				},
				{//自定义权限标识管理
					path: 'mark',
					component: mark
				},
				{//角色管理
					path: 'role',
					component: role
				},
				{//用户管理
					path: 'user',
					component: user
				},
			]
		},
		{
			path: '/Login', // 登录路由
			component: Login
		}

	]
})

router.beforeEach((to, from, next) => {
	/* 显示加载中动画 */
	store.commit(PAGELOADING, true)
	if (to.path == '/Login') {
		next()
	} else if (!storejs.get(STORECONST.USERINFO)) {
		next({
			path: '/Login',
			query: { redirect: to.fullPath }
		})
	} else {
		next()
	}
})

router.afterEach(route => {
	/* 隐藏加载中动画 */
	store.commit(PAGELOADING, false)
	// 修改 title 信息,兼容微信头部信息
	if (route.meta.title) {
		document.title = route.meta.title
		let ua = navigator.userAgent
		if (/\bMicroMessenger\/([\d.]+)/.test(ua) && /ip(hone|od|ad)/i.test(ua)) {
			var i = document.createElement('iframe')
			i.src = '/favicon.ico'
			i.style.display = 'none'
			i.onload = function () {
				setTimeout(function () {
					i.remove()
				}, 9)
			}
			document.body.appendChild(i)
		}
	}
})

Vue.use(Router)

export default router
